package com.atguigu.guli.service.edu.controller.api;


import com.atguigu.guli.service.base.dto.CourseDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Course;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.vo.ApiCourseDetailVo;
import com.atguigu.guli.service.edu.entity.vo.CourseQueryVo;
import com.atguigu.guli.service.edu.service.CourseService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author yz
 * @since 2022-07-18
 */
@RestController
@RequestMapping("/api/edu/course")

public class ApiCourseController {

    @Autowired
    CourseService courseService;
    @Autowired
    TeacherService teacherService;
    //1、根据讲师id查询课程列表
    @GetMapping("queryCoursesByTeacherId/{teacherId}")
    public R queryCoursesByTeacherId(@PathVariable("teacherId")String teacherId){
        List<Course> list = courseService.list(new LambdaQueryWrapper<Course>()
                .eq(Course::getTeacherId, teacherId));
        return R.ok().data("items",list);
    }
    //2.带条件查询课程列表接口
    @GetMapping
    public R queryGetCourse(CourseQueryVo courseQueryVo){
        List<Course> list= courseService.queryCourseByConditon(courseQueryVo);
        return R.ok().data("items",list);
    }
    //3.根据ID查询课程详情的接口
    @GetMapping("/{id}")
    public R getCourseDetailVo(@PathVariable("id") String id){
        ApiCourseDetailVo detail=courseService.getCourseDetailVo(id);
        return R.ok().data("item",detail);
    }
    //4.查询8门热门课程数据
    //@Cacheable(value = "ads",key = "'cache'")
    @GetMapping("/getHotCourses")
    public R getHotCourses(){
        List<Course> courses = courseService.list(new LambdaQueryWrapper<Course>()
                .select(Course::getId,
                        Course::getTitle,
                        Course::getCover,
                        Course::getPrice,
                        Course::getBuyCount,
                        Course::getViewCount).
                        orderByDesc(Course::getViewCount).
                        last("limit 8"));
        return R.ok().data("items",courses);
    }

    //5.查询订单课程信息
    @GetMapping("/getCourseDto/{id}")
    public R getCourseDto(@PathVariable("id") String id){
        Course course = courseService.getById(id);
        CourseDto courseDto = new CourseDto();
        BeanUtils.copyProperties(course,courseDto);
        //查询到的是讲师ID
        //通过讲师ID查询讲师姓名
        Teacher teacher = teacherService.getById(course.getTeacherId());
        courseDto.setTeacherName(teacher.getName());
        return R.ok().data("item",courseDto);

    }

}
